/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.commons.qbuilder;

import java.util.LinkedHashMap;
import java.util.Map;

import org.geoscheduler.commons.qbuilder.TableDecl.ColumnDecl;

/**
 * FIXME [lko] comment me!
 *
 * @author lko
 */
public class SQLValuesBuilder extends AbstractSQLBuilder<SQLValuesBuilder> {

	interface EvalDelegate {
		public void eval(StringBuilder bld, Map<String, String> pairs);
	}

	private final EvalDelegate delegate;

	public SQLValuesBuilder(EvalDelegate delegate) {
		this(new StringBuilder(), delegate);
	}

	public SQLValuesBuilder(StringBuilder bld, EvalDelegate delegate) {
		super(bld);
		this.delegate = delegate;
	}

	private Map<String, String> pairs = new LinkedHashMap<String, String>();

	public SQLValuesBuilder nValue(ColumnDecl<?, ?>... values) {
		return nValue(TableDecl.map(values));
	}

	public SQLValuesBuilder nValue(Map<String, String> values) {
		for (Map.Entry<String, String> entry : values.entrySet()) {
			nValue(entry.getKey(), entry.getValue());
		}
		return this;
	}

	public SQLValuesBuilder value(Map<String, String> values) {
		for (Map.Entry<String, String> entry : values.entrySet()) {
			value(entry.getKey(), entry.getValue());
		}
		return this;
	}

	public SQLValuesBuilder nValue(String column, String name) {
		return value(column, ":" + name);
	}

	public SQLValuesBuilder value(String column, String value) {
		pairs.put(column, value);
		return this;
	}

	@Override
	public String toString() {
		delegate.eval(builder(), pairs);
		return super.toString();
	}
}
